﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;

using FRL.DebtChased.Silverlight.Library;
using FRL.DebtChased.Silverlight.Library.RichContentService;
using System.ComponentModel;

namespace FRL.DebtChased.Silverlight.Library
{
    public partial class CommentsCtrl : UserControl, INotifyPropertyChanged
    {
        public CommentsCtrl()
        {
            InitializeComponent();
        }

        public event EventHandler<PanelExpandChangeEventArgs> ExpandChange;

        private GridLength _gridLengthZero = new GridLength(0);
        private GridLength _gridLenghtStar = new GridLength(1, GridUnitType.Star);
        private void HeaderBar_ExpandChange(object sender, EventArgs e)
        {
            if (HeaderBar.IsArrowUp)
            {
                HeaderBar.IsArrowUp = false;
                CommentsRow.Height = _gridLengthZero;
            }
            else
            {
                HeaderBar.IsArrowUp = true;
                CommentsRow.Height = _gridLenghtStar;
            }

            if (ExpandChange != null)
                ExpandChange(this, new PanelExpandChangeEventArgs(HeaderBar.IsArrowUp));
        }


                public event PropertyChangedEventHandler PropertyChanged;
        public void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        /////////////////////////////////////////////////////////////
        // Control "Parameter"

        private AccountTransport _account;
        public AccountTransport Account
        {
            get { return _account; }
            set
            {
                if ((object.ReferenceEquals(this._account, value) != true))
                {
                    this._account = value;
                    RaisePropertyChanged("Account");

                    ObjectNotes.Clear();
                    DebtorNotes.Source = Account.Notes;
                    this.DebtorNotes.View.Refresh();
                    this.AccountNotesPanel.ItemsSource = DebtorNotes.View;
                }
            }
        }

        CollectionViewSource DebtorNotes;

        private ObservableCollection<ObservableCollection<AccountNoteTransport>> _objectNotes = new ObservableCollection<ObservableCollection<AccountNoteTransport>>();
        public ObservableCollection<ObservableCollection<AccountNoteTransport>> ObjectNotes
        {
            get { return _objectNotes; }
            set
            {
                if ((object.ReferenceEquals(this._account, value) != true))
                {
                    this._objectNotes = value;
                    RaisePropertyChanged("ObjectNotes");
                }
            }
        }

        /////////////////////////////////////////////////////////////
        // Event Handlers

        private void AddCommentButton_Click(object sender, RoutedEventArgs e)
        {
            AddNoteDlg dlg = new AddNoteDlg(Account);
            dlg.Show();
        }

        private void OnObjectNoteDisplayRequest(object sender, ObjectNoteDisplayRequestEventArgs e)
        {
            if (e.Notes != null)
            {
                if (ObjectNotes.Contains(e.Notes))
                    ObjectNotes.Remove(e.Notes);
                else
                    ObjectNotes.Add(e.Notes);
            }
        }    

    }

    public class PanelExpandChangeEventArgs : EventArgs
    {
        public bool IsExpanded { get; set; }

        public PanelExpandChangeEventArgs(bool isExpanded)
        {
            IsExpanded = isExpanded;
        }
    }
}
